<?php

namespace app\common\model;

use think\Model;

class Store extends Model
{
    protected $primaryKey = 'store_id';
    protected $autoWriteTimestamp = true;
    protected $createTime = 'store_create_time';
    protected $updateTime = 'store_update_time';
    protected $rule = [
        ['store_category_id|店铺主营分类', ['require']],
        ['store_name|店铺名称', ['require']],
        ['store_face_img|店铺头像', ['require']],
        ['store_bg_img|店铺背景图', ['require']],
        ['store_introduce|店铺介绍', ['require', 'length:0,1000']],
    ];

    // 验证场景
    protected $scene = [
        'insert' => ['store_category_id', 'store_name', 'store_introduce', 'store_face_img', 'store_bg_img'],
        'update' => ['store_category_id', 'store_name', 'store_introduce', 'store_face_img', 'store_bg_img'],
    ];

    // 验证器
    public function intValidate()
    {
        $validate = new \think\Validate($this->rule);
        foreach ($this->scene as $k1 => $v1)
            $validate->scene($k1, $v1);
        return $validate;
    }

    protected static function init()
    {
        Store::beforeInsert(function ($store) {
            $store['store_verify_status'] = 1;
            $store['store_merchant_id'] = session('merchant')['merchant_id'];
        });
    }

    public function getStoreVerifyPasstimeAttr($value)
    {
        $value = date('Y-m-d H:i:s', $value);
        return $value;
    }

    public function getStoreOpenTimeAttr($value)
    {
        $value = date('Y-m-d H:i:s', $value);
        return $value;
    }


    public static function getStore($id)
    {
        $store = Store::find($id);
        return $store;

    }

    public static function getStoreByMerchantId($merchant_id)
    {
        $data = Store::where('store_merchant_id', $merchant_id)->find();
        return $data;
    }

    public function getStoreManagementTypeAttr($value)
    {
        $shop_type = [1 => '企业店', 2 => '旗舰店'];
        return $shop_type[$value];
    }

    public function getStoreCategoryIdAttr($value)
    {
        $value = db('goods_category')->field('goods_category_name')->find($value);
        $value = $value['goods_category_name'];
        return $value;
    }

    // 运营后台店铺信息列表
    public static function showlist($pagesize)
    {
        $where = [];
        $where['store_is_delete'] = ['eq', 0];
        // 关键字搜索(店铺名称，企业名称)
        $keyword = input('param.keyword');
        if ($keyword)
            $where['a.store_name|c.enterprise_name'] = ['like', "%$keyword%"];
        $data = Store::field('a.store_id,a.store_name,a.store_verify_status,a.store_verify_passtime,c.enterprise_id,c.enterprise_name')->alias('a')
            ->join('merchant b', 'a.store_merchant_id=b.merchant_id', 'left')
            ->join('enterprise c', 'b.merchant_id=c.enterprise_merchant_id', 'left')
            ->where($where)
            ->order('a.store_id', 'desc')
            ->paginate($pagesize);
        return $data;
    }

    public static function changeDel($store_id)
    {
        $result = Enterprise::where('store_id', $store_id)->setField('store_is_delete', 1);
        if (!$result)
            return false;

        return true;
    }

    public static function maxEnterpriseId()
    {
        $maxId = Enterprise::max('enterprise_id');
        return $maxId;
    }
}